package com.LachesismStorm.lachesism.member.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.LachesismStorm.lachesism.core.exception.Asserts;
import com.LachesismStorm.lachesism.core.service.RedisService;
import com.LachesismStorm.lachesism.core.util.CommonUtil;
import com.LachesismStorm.lachesism.core.util.JwtTokenUtil;
import com.LachesismStorm.lachesism.member.mapper.UmsUserMapper;
import com.LachesismStorm.lachesism.member.service.UmsMemberService;
import com.LachesismStorm.lachesism.member.util.MemberUtil;
import com.LachesismStorm.lachesism.member.vo.MemberDetails;
import com.LachesismStorm.lachesism.member.vo.UmsMember;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import static com.LachesismStorm.lachesism.core.common.enums.ResponseCodeEnum.*;


@Service
public class UmsMemberServiceImpl implements UmsMemberService {
    @Resource
    private UmsUserMapper umsUserMapper;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private JwtTokenUtil jwtTokenUtil;
    @Resource
    private RedisService redisService;

    @Value("${aes.key}")
    private String key;

    @Value("${file.dating.address}")
    private String datAddress;

    @Value("${url.pc}")
    private String webUrl;


    @Override
    public UserDetails loadUserByUsername(String username) {
        UmsMember member = umsUserMapper.getMemberInfo(username);
        if (member != null) {
            return new MemberDetails(member);
        }
        return null;
    }

    @Override
    public String register(UmsMember member) {
        //1查看用户是否存在
        Integer count = umsUserMapper.checkUserExists(member.getPhone());
        if (count > 0) {
            Asserts.fail(EXIST);
        } else {
            //2.注册后登陆
            UmsMember umsMember = new UmsMember();
            BeanUtil.copyProperties(member, umsMember);
            umsMember.setPassword(passwordEncoder.encode(umsMember.getPassword()));
            umsMember.setNickName("用户" + umsMember.getPhone().substring(7));
            umsUserMapper.register(umsMember);
        }
        return login(member);
    }

    @Override
    public UmsMember getUserInfo() {
        Integer userId = MemberUtil.getUserId();
        return umsUserMapper.getUserInfo(userId);
    }

    @Override
    public String login(UmsMember member) {
        UmsMember user = umsUserMapper.getMemberInfoByPhone(member.getPhone());
        if (user == null) {
            Asserts.fail(NOTFOUND);
        }
        if (user.getStatus() == 0) {
            Asserts.fail(NOT_ELIGIBLE);
        }
        if (!passwordEncoder.matches(member.getPassword(), user.getPassword())) {
            Asserts.fail(PW_ERROR);
        }
        MemberDetails memberDetails = new MemberDetails(user);
        return jwtTokenUtil.generateToken(memberDetails);
    }

    @Override
    public void updateInfo(UmsMember member) {
        if (member.getPassword() != null) {
            member.setPassword(passwordEncoder.encode(member.getPassword()));
        }
        umsUserMapper.updateUserInfo(member);
    }

}